import './common/base.scss'
import 'bootstrap/dist/css/bootstrap.min.css'
import './sass/login.scss'

//import { setSubmitState } from './common/utils'
import axios from 'axios'

let form =document.querySelector('form')
let submit=document.querySelector('.submit')
let username=document.querySelector('#exampleInputUsername')
let password=document.querySelector('#exampleInputPassword1')
console.log(form,username,password);

let flag={
    userTest:false,
    passTest:false
}
username.onblur=()=>{
    let reg=/^[a-z\u4e00-\u9fa5]{2,10}$/
    flag.userTest=reg.test(username.value)

    let arr=Object.values(flag)
    let f=arr.every(item=>{
        return item==true
    })
    console.log(arr);
    submit.disabled=!f
}
password.onblur=()=>{
    let reg=/^[0-9a-z]{6,12}$/
    flag.passTest=reg.test(password.value)

    let arr=Object.values(flag)
    let f=arr.every(item=>{
        return item==true
    })
    console.log(arr);
    submit.disabled=!f
}
form.addEventListener('submit',async(e)=>{
    e.preventDefault()
    let {data}=await axios({
        method:'post',
        url:'/api/user/login',
        data:{
            username:username.value,
            password:password.value
        }
    })
    if(data.code==400){
        alert(data.msg)
        return
    }
    console.log(data);
    location.href='../index.html'
   localStorage.setItem('token',data.token)
})